<?php
include('Data.php');
class vm {
    
    //Déclarations des variables des infos générales
    public $name;
    public $os;
    public $ram;
    public $mac;
    public $state;
    public $owner;
    //Déclarations des variables spécifiques à la machine virtuelle et au système 
    public $local_ip;

    //Déclarations des variables des infos mysql
    private $sql_id;
    private $vnc_port;
    

    public function __construct($name) {
        $this->name = $name;
        $this->get_info();
        $this->get_sql();
    }
    
    public function get_info() {
        
        exec("vboxmanage showvminfo ".$this->name, $raw_info);
        
        //On parse la chaine pour n'extraire que les infos importantes
        $pattern = "/([a-zA-Z0-9\s\(\)\.-]*)[:+\s]+([a-zA-Z0-9 \,\(\)\/\.:-]+)/";
        $info = array();
                
        foreach ($raw_info as $key => $value) {
            
            preg_match_all($pattern,$value,$matches);
                        
            // On remplie nos valeurs de classes
            if(stristr($matches[1][0],"Guest os")) { $this->os = $matches[2][0];}
            if(stristr($matches[1][0],"Memory size")) { $this->ram = $matches[2][0];}
            
            if(strstr($matches[2][0],"MAC:")) { 
                $pat1 = "/([a-zA-Z0-9\s\(\)\.-]*)[:+\s]+([a-zA-Z0-9 \(\)\/\.:-]+)/";
                
                $mac = $matches[2][0];
                preg_match($pat1,$mac,$matches1);
                
                $this->mac = $matches1[2];
                
            }
            if(stristr($matches[1][0],"state")) { $this->state = $matches[2][0];}
            
            //On supprime le tableau $matches
            unset($matches);
                                   
        } 
	   //$this->local_ip = echo exec("nmap 192.168.1.10-200 ; arp -a | grep ".$this->mac." | awk '{print $2}'"); 
	   //$this->local_ip = echo exec("nmap 192.168.1.1-255 ; /usr/sbin/arp | grep 00:14:2a:22:c3:81 | awk '{print $1}'");
    }
    
    public function get_sql(){
        
        $query = mysql_query("SELECT * FROM machine_virtuel WHERE name=$this->name ");
        $query_result = mysql_fetch_assoc($query);
        
        //On remplie les attributs
        $this->sql_id = $query_result['Id_machine'];
        $this->vnc_port = 56000 + $this->sql_id;

    }
    
    public function get_ip(){
        
    }
    
    public function state() {
        //Retourne TRUE si la machine est allumée et FALSE si elle est éteinte ou pour tout autre état.
        $this->get_info();
        
        if(strstr($this->state,"powered off")){
            return FALSE;
        }
        elseif(strstr($this->state,"running")){
            return TRUE;
        }
        else return FALSE;
    }
    
    public function create() {
        
    }
    
    public function start() {
        
        if(exec ("vboxmanage startvm $this->name --type headless")){
            return TRUE;
        }
        else return FALSE;
    }
    
    public function stop() {
        
        exec ("vboxmanage controlvm $this->name poweroff");
        
    }
}

?>
